package de.velvettear.bruteforce.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import de.velvettear.bruteforce.kernel.Charset;
import de.velvettear.bruteforce.kernel.Database;
import de.velvettear.bruteforce.kernel.Statics;
import de.velvettear.bruteforce.kernel.WordList;
import de.velvettear.bruteforce.util.Log;

public class Menu extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JTextField tf_continue;
	private JTextField tf_file;
	private JProgressBar progressbar;
	private JButton btn_start;

	private JTextArea ta_lowercase;
	private JTextArea ta_uppercase;
	private JTextArea ta_numeric;
	private JTextArea ta_special;
	private JTextArea ta_log;
	private JCheckBox cb_lowercase;
	private JCheckBox cb_uppercase;
	private JCheckBox cb_numeric;
	private JCheckBox cb_special;
	private JPanel panel_output;
	private JRadioButton rb_file;
	private JRadioButton rb_mysql;
	private JPanel panel_file;
	private JPanel panel_mysql;
	private JLabel lbl_hostname;
	private JLabel lbl_username;
	private JLabel lbl_password;
	private JTextField tf_hostname;
	private JTextField tf_username;
	private JPasswordField tf_password;
	private JPanel panel_continue;
	private JCheckBox cb_continue;
	private JSpinner sp_length;
	private JPanel panel_length;
	private JButton btn_test;
	private JLabel lbl_test;
	
	public Menu() { 
		this.setVisible(false);
		setTitle(Statics.NAME_APPLICATION);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 350);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);
				
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setForeground(Color.WHITE);
		tabbedPane.setFont(new Font("Quartz MS", Font.BOLD, 16));
		tabbedPane.setBorder(null);
		tabbedPane.setBackground(Color.DARK_GRAY);
		GridBagConstraints gbc_tabbedPane_1 = new GridBagConstraints();
		gbc_tabbedPane_1.gridwidth = 2;
		gbc_tabbedPane_1.insets = new Insets(0, 0, 5, 0);
		gbc_tabbedPane_1.fill = GridBagConstraints.BOTH;
		gbc_tabbedPane_1.gridx = 0;
		gbc_tabbedPane_1.gridy = 0;
		contentPane.add(tabbedPane, gbc_tabbedPane_1);

		JPanel panel_wordlist = new JPanel();
		panel_wordlist.setBackground(Color.DARK_GRAY);
		tabbedPane.addTab("Wordlist", null, panel_wordlist, null);
		GridBagLayout gbl_panel_wordlist = new GridBagLayout();
		gbl_panel_wordlist.columnWidths = new int[] { 0, 0, 0 };
		gbl_panel_wordlist.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 100, 0 };
		gbl_panel_wordlist.columnWeights = new double[] { 1.0, 1.0,	Double.MIN_VALUE };
		gbl_panel_wordlist.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,	1.0, Double.MIN_VALUE };
		panel_wordlist.setLayout(gbl_panel_wordlist);
				
		panel_length = new JPanel();
		panel_length.setBackground(Color.DARK_GRAY);
		GridBagConstraints gbc_panel_length = new GridBagConstraints();
		gbc_panel_length.insets = new Insets(5, 0, 25, 0);
		gbc_panel_length.gridwidth = 2;
		gbc_panel_length.fill = GridBagConstraints.BOTH;
		gbc_panel_length.gridx = 0;
		gbc_panel_length.gridy = 0;
		panel_wordlist.add(panel_length, gbc_panel_length);
		GridBagLayout gbl_panel_length = new GridBagLayout();
		gbl_panel_length.columnWidths = new int[]{121, 164, 0};
		gbl_panel_length.rowHeights = new int[]{22, 0};
		gbl_panel_length.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_length.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_length.setLayout(gbl_panel_length);
			
		JLabel lbl_length = new JLabel("Maximum length:");
		GridBagConstraints gbc_lbl_length = new GridBagConstraints();
		gbc_lbl_length.anchor = GridBagConstraints.WEST;
		gbc_lbl_length.insets = new Insets(0, 5, 0, 5);
		gbc_lbl_length.gridx = 0;
		gbc_lbl_length.gridy = 0;
		panel_length.add(lbl_length, gbc_lbl_length);
		lbl_length.setForeground(Color.WHITE);
		lbl_length.setFont(new Font("Quartz MS", Font.BOLD, 16));
		
		sp_length = new JSpinner();
		GridBagConstraints gbc_sp_length = new GridBagConstraints();
		gbc_sp_length.insets = new Insets(0, 5, 0, 5);
		gbc_sp_length.fill = GridBagConstraints.HORIZONTAL;
		gbc_sp_length.anchor = GridBagConstraints.NORTH;
		gbc_sp_length.gridx = 1;
		gbc_sp_length.gridy = 0;
		panel_length.add(sp_length, gbc_sp_length);
		sp_length.setFont(new Font("Calibri", Font.BOLD, 12));
		sp_length.setModel(new SpinnerListModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
		JSpinner.DefaultEditor editor = new JSpinner.DefaultEditor(sp_length);
		editor.getTextField().setBackground(Statics.COLOR_FIELD_DEFAULT);
		sp_length.setEditor(editor);
		
		panel_output = new JPanel();
		panel_output.setBackground(Color.DARK_GRAY);
		GridBagConstraints gbc_panel_output = new GridBagConstraints();
		gbc_panel_output.insets = new Insets(0, 0, 5, 0);
		gbc_panel_output.gridwidth = 2;
		gbc_panel_output.fill = GridBagConstraints.BOTH;
		gbc_panel_output.gridx = 0;
		gbc_panel_output.gridy = 1;
		panel_wordlist.add(panel_output, gbc_panel_output);
		GridBagLayout gbl_panel_output = new GridBagLayout();
		gbl_panel_output.columnWidths = new int[]{0, 0, 0};
		gbl_panel_output.rowHeights = new int[]{0, 0, 0};
		gbl_panel_output.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_panel_output.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		panel_output.setLayout(gbl_panel_output);
		
		JLabel lbl_output = new JLabel("Output:");
		GridBagConstraints gbc_lbl_output = new GridBagConstraints();
		gbc_lbl_output.anchor = GridBagConstraints.WEST;
		gbc_lbl_output.fill = GridBagConstraints.VERTICAL;
		gbc_lbl_output.gridwidth = 2;
		gbc_lbl_output.insets = new Insets(0, 5, 0, 5);
		gbc_lbl_output.gridx = 0;
		gbc_lbl_output.gridy = 0;
		panel_output.add(lbl_output, gbc_lbl_output);
		lbl_output.setForeground(Color.WHITE);
		lbl_output.setFont(new Font("Quartz MS", Font.BOLD, 16));
		
		rb_mysql = new JRadioButton("MySQL");
		rb_mysql.setBackground(Color.DARK_GRAY);
		rb_mysql.setForeground(Color.WHITE);
		rb_mysql.setSelected(true);
		GridBagConstraints gbc_rb_mysql = new GridBagConstraints();
		gbc_rb_mysql.fill = GridBagConstraints.VERTICAL;
		gbc_rb_mysql.anchor = GridBagConstraints.WEST;
		gbc_rb_mysql.insets = new Insets(0, 5, 0, 5);
		gbc_rb_mysql.gridx = 0;
		gbc_rb_mysql.gridy = 1;
		panel_output.add(rb_mysql, gbc_rb_mysql);
		rb_mysql.addActionListener(this);
		rb_mysql.setActionCommand(Statics.ACTION_OUTPUTMYSQL);
				
		rb_file = new JRadioButton("File");
		rb_file.setBackground(Color.DARK_GRAY);
		rb_file.setForeground(Color.WHITE);
		rb_file.setSelected(false);
		GridBagConstraints gbc_rb_file = new GridBagConstraints();
		gbc_rb_file.insets = new Insets(0, 5, 0, 5);
		gbc_rb_file.fill = GridBagConstraints.VERTICAL;
		gbc_rb_file.anchor = GridBagConstraints.WEST;
		gbc_rb_file.gridx = 1;
		gbc_rb_file.gridy = 1;
		panel_output.add(rb_file, gbc_rb_file);
		rb_file.addActionListener(this);
		rb_file.setActionCommand(Statics.ACTION_OUTPUTFILE);
		
		panel_file = new JPanel();
		panel_file.setBackground(Color.DARK_GRAY);
		panel_file.setVisible(false);
		GridBagConstraints gbc_panel_file = new GridBagConstraints();
		gbc_panel_file.insets = new Insets(0, 0, 25, 0);
		gbc_panel_file.gridwidth = 2;
		gbc_panel_file.fill = GridBagConstraints.BOTH;
		gbc_panel_file.gridx = 0;
		gbc_panel_file.gridy = 2;
		panel_wordlist.add(panel_file, gbc_panel_file);
		GridBagLayout gbl_panel_file = new GridBagLayout();
		gbl_panel_file.columnWidths = new int[]{0, 0, 0};
		gbl_panel_file.rowHeights = new int[]{0, 0};
		gbl_panel_file.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_file.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panel_file.setLayout(gbl_panel_file);
				
		JButton btn_folderchooser = new JButton("Choose folder");
		GridBagConstraints gbc_btn_folderchooser = new GridBagConstraints();
		gbc_btn_folderchooser.anchor = GridBagConstraints.WEST;
		gbc_btn_folderchooser.fill = GridBagConstraints.VERTICAL;
		gbc_btn_folderchooser.insets = new Insets(0, 5, 0, 5);
		gbc_btn_folderchooser.gridx = 0;
		gbc_btn_folderchooser.gridy = 0;
		panel_file.add(btn_folderchooser, gbc_btn_folderchooser);
		btn_folderchooser.setFont(new Font("Quartz MS", Font.BOLD, 14));
		btn_folderchooser.addActionListener(this);
		btn_folderchooser.setActionCommand(Statics.ACTION_CHOOSEFOLDER);
		
		tf_file = new JTextField();		
		GridBagConstraints gbc_tf_file = new GridBagConstraints();
		gbc_tf_file.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_file.insets = new Insets(0, 5, 0, 5);
		gbc_tf_file.gridx = 1;
		gbc_tf_file.gridy = 0;
		panel_file.add(tf_file, gbc_tf_file);
		tf_file.setFont(new Font("Calibri", Font.BOLD, 12));
		tf_file.setColumns(10);
		tf_file.setBackground(Statics.COLOR_FIELD_DEFAULT);
		tf_file.setEditable(false);
		
		panel_mysql = new JPanel();
		panel_mysql.setBackground(Color.DARK_GRAY);
		panel_mysql.setVisible(true);
		GridBagConstraints gbc_panel_mysql = new GridBagConstraints();
		gbc_panel_mysql.insets = new Insets(0, 0, 25, 0);
		gbc_panel_mysql.gridwidth = 2;
		gbc_panel_mysql.fill = GridBagConstraints.BOTH;
		gbc_panel_mysql.gridx = 0;
		gbc_panel_mysql.gridy = 3;
		panel_wordlist.add(panel_mysql, gbc_panel_mysql);
		GridBagLayout gbl_panel_mysql = new GridBagLayout();
		gbl_panel_mysql.columnWidths = new int[]{0, 0, 0};
		gbl_panel_mysql.rowHeights = new int[]{0, 0, 0, 0, 0};
		gbl_panel_mysql.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_mysql.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel_mysql.setLayout(gbl_panel_mysql);
		
		lbl_hostname = new JLabel("Hostname:");
		lbl_hostname.setFont(new Font("Quartz MS", Font.BOLD, 16));
		lbl_hostname.setForeground(Color.WHITE);
		GridBagConstraints gbc_lbl_hostname = new GridBagConstraints();
		gbc_lbl_hostname.anchor = GridBagConstraints.WEST;
		gbc_lbl_hostname.insets = new Insets(0, 5, 5, 5);
		gbc_lbl_hostname.gridx = 0;
		gbc_lbl_hostname.gridy = 0;
		panel_mysql.add(lbl_hostname, gbc_lbl_hostname);
		
		tf_hostname = new JTextField();
		tf_hostname.setFont(new Font("Calibri", Font.BOLD, 12));
		GridBagConstraints gbc_tf_hostname = new GridBagConstraints();
		gbc_tf_hostname.insets = new Insets(0, 5, 5, 0);
		gbc_tf_hostname.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_hostname.gridx = 1;
		gbc_tf_hostname.gridy = 0;
		panel_mysql.add(tf_hostname, gbc_tf_hostname);
		tf_hostname.setColumns(10);
		tf_hostname.setBackground(Statics.COLOR_FIELD_DEFAULT);
		
		lbl_username = new JLabel("Username:");
		lbl_username.setFont(new Font("Quartz MS", Font.BOLD, 16));
		lbl_username.setForeground(Color.WHITE);
		GridBagConstraints gbc_lbl_username = new GridBagConstraints();
		gbc_lbl_username.insets = new Insets(0, 5, 5, 5);
		gbc_lbl_username.anchor = GridBagConstraints.WEST;
		gbc_lbl_username.gridx = 0;
		gbc_lbl_username.gridy = 1;
		panel_mysql.add(lbl_username, gbc_lbl_username);
		
		tf_username = new JTextField();
		tf_username.setFont(new Font("Calibri", Font.BOLD, 12));
		GridBagConstraints gbc_tf_username = new GridBagConstraints();
		gbc_tf_username.insets = new Insets(0, 5, 5, 0);
		gbc_tf_username.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_username.gridx = 1;
		gbc_tf_username.gridy = 1;
		panel_mysql.add(tf_username, gbc_tf_username);
		tf_username.setColumns(10);
		tf_username.setBackground(Statics.COLOR_FIELD_DEFAULT);
		
		lbl_password = new JLabel("Password:");
		lbl_password.setForeground(Color.WHITE);
		lbl_password.setFont(new Font("Quartz MS", Font.BOLD, 16));
		GridBagConstraints gbc_lbl_password = new GridBagConstraints();
		gbc_lbl_password.anchor = GridBagConstraints.WEST;
		gbc_lbl_password.insets = new Insets(0, 5, 5, 5);
		gbc_lbl_password.gridx = 0;
		gbc_lbl_password.gridy = 2;
		panel_mysql.add(lbl_password, gbc_lbl_password);
		
		tf_password = new JPasswordField();
		tf_password.setFont(new Font("Calibri", Font.BOLD, 12));
		GridBagConstraints gbc_tf_password = new GridBagConstraints();
		gbc_tf_password.insets = new Insets(0, 5, 5, 0);
		gbc_tf_password.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_password.gridx = 1;
		gbc_tf_password.gridy = 2;
		panel_mysql.add(tf_password, gbc_tf_password);
		tf_password.setBackground(Statics.COLOR_FIELD_DEFAULT);
		
		btn_test = new JButton("Test connection");
		btn_test.setFont(new Font("Quartz MS", Font.BOLD, 14));
		GridBagConstraints gbc_btn_test = new GridBagConstraints();
		gbc_btn_test.fill = GridBagConstraints.VERTICAL;
		gbc_btn_test.anchor = GridBagConstraints.WEST;
		gbc_btn_test.insets = new Insets(0, 5, 0, 5);
		gbc_btn_test.gridx = 0;
		gbc_btn_test.gridy = 3;
		panel_mysql.add(btn_test, gbc_btn_test);
		btn_test.addActionListener(this);
		btn_test.setActionCommand(Statics.ACTION_TESTMYSQL);
		
		lbl_test = new JLabel("");
		lbl_test.setForeground(Color.WHITE);
		lbl_test.setFont(new Font("Quartz MS", Font.BOLD, 16));
		GridBagConstraints gbc_lbl_test = new GridBagConstraints();
		gbc_lbl_test.gridx = 1;
		gbc_lbl_test.gridy = 3;
		panel_mysql.add(lbl_test, gbc_lbl_test);
		lbl_test.setVisible(false);
	
		panel_continue = new JPanel();
		panel_continue.setBackground(Color.DARK_GRAY);
		GridBagConstraints gbc_panel_continue = new GridBagConstraints();
		gbc_panel_continue.insets = new Insets(0, 0, 25, 0);
		gbc_panel_continue.gridwidth = 2;
		gbc_panel_continue.fill = GridBagConstraints.BOTH;
		gbc_panel_continue.gridx = 0;
		gbc_panel_continue.gridy = 4;
		panel_wordlist.add(panel_continue, gbc_panel_continue);
		GridBagLayout gbl_panel_continue = new GridBagLayout();
		gbl_panel_continue.columnWidths = new int[]{0, 0, 0};
		gbl_panel_continue.rowHeights = new int[]{0, 0};
		gbl_panel_continue.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_continue.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_continue.setLayout(gbl_panel_continue);
		
		cb_continue = new JCheckBox("Continue List");
		GridBagConstraints gbc_cb_continue = new GridBagConstraints();
		gbc_cb_continue.anchor = GridBagConstraints.WEST;
		gbc_cb_continue.insets = new Insets(0, 5, 0, 5);
		gbc_cb_continue.gridx = 0;
		gbc_cb_continue.gridy = 0;
		panel_continue.add(cb_continue, gbc_cb_continue);
		cb_continue.setForeground(Color.WHITE);
		cb_continue.setFont(new Font("Quartz MS", Font.BOLD, 16));
		cb_continue.setBackground(Color.DARK_GRAY);
		cb_continue.addActionListener(this);
		cb_continue.setActionCommand(Statics.ACTION_CONTINUE);
		
		tf_continue = new JTextField();
		tf_continue.setEditable(false);
		GridBagConstraints gbc_tf_start = new GridBagConstraints();
		gbc_tf_start.insets = new Insets(0, 5, 0, 5);
		gbc_tf_start.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_start.gridx = 1;
		gbc_tf_start.gridy = 0;
		panel_continue.add(tf_continue, gbc_tf_start);
		tf_continue.setFont(new Font("Calibri", Font.BOLD, 12));
		tf_continue.setColumns(10);
		tf_continue.setBackground(Statics.COLOR_FIELD_DEFAULT);
		tf_continue.setVisible(false);

		JLabel lblCharacters = new JLabel("Characters:");
		lblCharacters.setForeground(Color.WHITE);
		lblCharacters.setFont(new Font("Quartz MS", Font.BOLD, 16));
		GridBagConstraints gbc_lblCharacters = new GridBagConstraints();
		gbc_lblCharacters.anchor = GridBagConstraints.WEST;
		gbc_lblCharacters.insets = new Insets(0, 5, 5, 5);
		gbc_lblCharacters.gridx = 0;
		gbc_lblCharacters.gridy = 5;
		panel_wordlist.add(lblCharacters, gbc_lblCharacters);

		JPanel panel_characters = new JPanel();
		panel_characters.setBackground(Color.LIGHT_GRAY);
		panel_characters.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		GridBagConstraints gbc_panel_characters = new GridBagConstraints();
		gbc_panel_characters.gridwidth = 2;
		gbc_panel_characters.insets = new Insets(5, 5, 0, 0);
		gbc_panel_characters.fill = GridBagConstraints.BOTH;
		gbc_panel_characters.gridx = 0;
		gbc_panel_characters.gridy = 6;
		panel_wordlist.add(panel_characters, gbc_panel_characters);
		GridBagLayout gbl_panel_characters = new GridBagLayout();
		gbl_panel_characters.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gbl_panel_characters.rowHeights = new int[] { 0, 0, 0 };
		gbl_panel_characters.columnWeights = new double[] { 1.0, 1.0, 1.0, 1.0,
				Double.MIN_VALUE };
		gbl_panel_characters.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		panel_characters.setLayout(gbl_panel_characters);

		cb_lowercase = new JCheckBox("Lowercase");
		cb_lowercase.setSelected(true);
		cb_lowercase.setForeground(Color.BLACK);
		cb_lowercase.setBackground(Color.LIGHT_GRAY);
		cb_lowercase.setFont(new Font("Quartz MS", Font.PLAIN, 12));
		GridBagConstraints gbc_cb_lowercase = new GridBagConstraints();
		gbc_cb_lowercase.gridx = 0;
		gbc_cb_lowercase.gridy = 0;
		panel_characters.add(cb_lowercase, gbc_cb_lowercase);

		cb_uppercase = new JCheckBox("Uppercase");
		cb_uppercase.setSelected(true);
		cb_uppercase.setBackground(Color.LIGHT_GRAY);
		cb_uppercase.setFont(new Font("Quartz MS", Font.PLAIN, 12));
		GridBagConstraints gbc_cb_uppercase = new GridBagConstraints();
		gbc_cb_uppercase.gridx = 1;
		gbc_cb_uppercase.gridy = 0;
		panel_characters.add(cb_uppercase, gbc_cb_uppercase);

		cb_numeric = new JCheckBox("Numeric");
		cb_numeric.setSelected(true);
		cb_numeric.setBackground(Color.LIGHT_GRAY);
		cb_numeric.setFont(new Font("Quartz MS", Font.PLAIN, 12));
		GridBagConstraints gbc_cb_numeric = new GridBagConstraints();
		gbc_cb_numeric.gridx = 2;
		gbc_cb_numeric.gridy = 0;
		panel_characters.add(cb_numeric, gbc_cb_numeric);

		cb_special = new JCheckBox("Special");
		cb_special.setSelected(true);
		cb_special.setBackground(Color.LIGHT_GRAY);
		cb_special.setFont(new Font("Quartz MS", Font.PLAIN, 12));
		GridBagConstraints gbc_cb_special = new GridBagConstraints();
		gbc_cb_special.gridx = 3;
		gbc_cb_special.gridy = 0;
		panel_characters.add(cb_special, gbc_cb_special);

		ta_lowercase = new JTextArea();
		ta_lowercase.setEditable(false);
		ta_lowercase.setFont(new Font("Calibri", Font.BOLD, 12));
		ta_lowercase.setLineWrap(true);
		ta_lowercase.setBackground(Statics.COLOR_FIELD_DEFAULT);
		ta_uppercase = new JTextArea();
		ta_uppercase.setEditable(false);
		ta_uppercase.setFont(new Font("Calibri", Font.BOLD, 12));
		ta_uppercase.setLineWrap(true);
		ta_uppercase.setBackground(Statics.COLOR_FIELD_DEFAULT);
		ta_numeric = new JTextArea();
		ta_numeric.setEditable(false);
		ta_numeric.setFont(new Font("Calibri", Font.BOLD, 12));
		ta_numeric.setLineWrap(true);
		ta_numeric.setBackground(Statics.COLOR_FIELD_DEFAULT);
		ta_special = new JTextArea();
		ta_special.setEditable(false);
		ta_special.setFont(new Font("Calibri", Font.BOLD, 12));
		ta_special.setLineWrap(true);
		ta_special.setBackground(Statics.COLOR_FIELD_DEFAULT);

		JScrollPane scrollpane_lowercase = new JScrollPane(ta_lowercase);
		GridBagConstraints gbc_scrollpane_lowercase = new GridBagConstraints();
		gbc_scrollpane_lowercase.insets = new Insets(1, 1, 1, 1);
		gbc_scrollpane_lowercase.fill = GridBagConstraints.BOTH;
		gbc_scrollpane_lowercase.gridx = 0;
		gbc_scrollpane_lowercase.gridy = 1;
		panel_characters.add(scrollpane_lowercase, gbc_scrollpane_lowercase);

		JScrollPane scrollpane_uppercase = new JScrollPane(ta_uppercase);
		GridBagConstraints gbc_scrollpane_uppercase = new GridBagConstraints();
		gbc_scrollpane_uppercase.insets = new Insets(1, 1, 1, 1);
		gbc_scrollpane_uppercase.fill = GridBagConstraints.BOTH;
		gbc_scrollpane_uppercase.gridx = 1;
		gbc_scrollpane_uppercase.gridy = 1;
		panel_characters.add(scrollpane_uppercase, gbc_scrollpane_uppercase);

		JScrollPane scrollpane_numeric = new JScrollPane(ta_numeric);
		GridBagConstraints gbc_scrollpane_numeric = new GridBagConstraints();
		gbc_scrollpane_numeric.insets = new Insets(1, 1, 1, 1);
		gbc_scrollpane_numeric.fill = GridBagConstraints.BOTH;
		gbc_scrollpane_numeric.gridx = 2;
		gbc_scrollpane_numeric.gridy = 1;
		panel_characters.add(scrollpane_numeric, gbc_scrollpane_numeric);

		JScrollPane scrollpane_special = new JScrollPane(ta_special);
		GridBagConstraints gbc_scrollpane_special = new GridBagConstraints();
		gbc_scrollpane_special.insets = new Insets(1, 1, 1, 1);
		gbc_scrollpane_special.fill = GridBagConstraints.BOTH;
		gbc_scrollpane_special.gridx = 3;
		gbc_scrollpane_special.gridy = 1;
		panel_characters.add(scrollpane_special, gbc_scrollpane_special);

		JPanel panel_bruteforce = new JPanel();
		tabbedPane.addTab("Bruteforce", null, panel_bruteforce, null);

		JPanel panel_log = new JPanel();
		panel_log.setBackground(Color.DARK_GRAY);
		tabbedPane.addTab("Log", null, panel_log, null);
		GridBagLayout gbl_panel_log = new GridBagLayout();
		gbl_panel_log.columnWidths = new int[] { 217, 0 };
		gbl_panel_log.rowHeights = new int[] { 22, 0 };
		gbl_panel_log.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_panel_log.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		panel_log.setLayout(gbl_panel_log);
		ta_log = new JTextArea();
		ta_log.setEditable(false);
		ta_log.setFont(new Font("Calibri", Font.BOLD, 12));

		JScrollPane scrollPane_log = new JScrollPane(ta_log);
		GridBagConstraints gbc_scrollPane_log = new GridBagConstraints();
		gbc_scrollPane_log.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_log.gridx = 0;
		gbc_scrollPane_log.gridy = 0;
		panel_log.add(scrollPane_log, gbc_scrollPane_log);

		progressbar = new JProgressBar();
		progressbar.setForeground(Statics.COLOR_PROGRESSBAR);
		progressbar.setBackground(Color.DARK_GRAY);
		progressbar.setFont(new Font("Quartz MS", Font.BOLD, 16));
		GridBagConstraints gbc_progressbar = new GridBagConstraints();
		gbc_progressbar.insets = new Insets(5, 0, 0, 5);
		gbc_progressbar.fill = GridBagConstraints.BOTH;
		gbc_progressbar.gridx = 0;
		gbc_progressbar.gridy = 1;
		contentPane.add(progressbar, gbc_progressbar);
		progressbar.setMinimum(0);
		progressbar.setMaximum(100);

		btn_start = new JButton("Start");
		btn_start.setFont(new Font("Quartz MS", Font.BOLD, 14));
		GridBagConstraints gbc_btn_start = new GridBagConstraints();
		gbc_btn_start.insets = new Insets(5, 0, 0, 0);
		gbc_btn_start.anchor = GridBagConstraints.EAST;
		gbc_btn_start.gridx = 1;
		gbc_btn_start.gridy = 1;
		contentPane.add(btn_start, gbc_btn_start);
		progressbar.setVisible(false);
		btn_start.addActionListener(this);
		btn_start.setActionCommand(Statics.ACTION_START);
				
		setLookAndFeel(true);
		setDefaultValues();

		Log.activate(ta_log);
		
		pack();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
	}

	private void setDefaultValues() {
		String lowercase = "";
		for (char tmp : Charset.chars_lowercase) {
			lowercase += tmp + " ";
		}
		lowercase = lowercase.substring(0, lowercase.lastIndexOf(" "));

		String uppercase = "";
		for (char tmp : Charset.chars_uppercase) {
			uppercase += tmp + " ";
		}
		uppercase = uppercase.substring(0, uppercase.lastIndexOf(" "));

		String numeric = "";
		for (char tmp : Charset.chars_numeric) {
			numeric += tmp + " ";
		}
		numeric = numeric.substring(0, numeric.lastIndexOf(" "));

		String special = "";
		for (char tmp : Charset.chars_special) {
			special += tmp + " ";
		}
		special = special.substring(0, special.lastIndexOf(" "));

		ta_lowercase.setText(lowercase);
		ta_uppercase.setText(uppercase);
		ta_numeric.setText(numeric);
		ta_special.setText(special);
	}

	private void setLookAndFeel(boolean motif) {
		// Available Look and Feels
		// "javax.swing.plaf.metal.MetalLookAndFeel"
		// "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
		try {
			if (motif) {
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
			} else {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean checkSettings() {
		boolean allSet = true;
		
		if (sp_length.getValue().toString().trim().length() == 0) {
			sp_length.setBackground(Statics.COLOR_FIELD_ERROR);
			allSet = false;
		} else {
			sp_length.setBackground(Statics.COLOR_FIELD_DEFAULT);
		}
		if (rb_mysql.isSelected()) {
			if (tf_hostname.getText().toString().trim().length() == 0) {
				tf_hostname.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				tf_hostname.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
			if (tf_username.getText().toString().trim().length() == 0) {
				tf_username.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				tf_username.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
			if (String.valueOf(tf_password.getPassword()).trim().length() == 0) {
				tf_password.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				tf_password.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
		} else if (rb_file.isSelected()) {
			if (tf_file.getText().toString().trim().length() == 0) {
				tf_file.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				tf_file.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
		}
		if (!cb_lowercase.isSelected() && !cb_uppercase.isSelected() && !cb_numeric.isSelected() && !cb_special.isSelected()) {
			if (!cb_lowercase.isSelected()) {
				ta_lowercase.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				ta_lowercase.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
			if (!cb_uppercase.isSelected()) {
				ta_uppercase.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				ta_uppercase.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
			if (!cb_numeric.isSelected()) {
				ta_numeric.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				ta_numeric.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
			if (!cb_special.isSelected()) {
				ta_special.setBackground(Statics.COLOR_FIELD_ERROR);
				allSet = false;
			} else {
				ta_special.setBackground(Statics.COLOR_FIELD_DEFAULT);
			}
		}
		
		return allSet;		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(Statics.ACTION_START)) {			
			if (WordList.isFilling()) {
				WordList.stopFilling();
				setTitle(Statics.NAME_APPLICATION);
				btn_start.setText("Start");
			} else {
				if (tabbedPane.getSelectedIndex() == 0) {
					char[] charset = Charset.getCharset(cb_lowercase.isSelected(), cb_uppercase.isSelected(), cb_numeric.isSelected(), cb_special.isSelected());
					
					if (rb_file.isSelected()) {
						if (new File(tf_file.getText()).exists()) {
							if (JOptionPane.showConfirmDialog(this, "Wordlist is already existing!\nDo you want to overwrite it?", "Overwrite wordlist", JOptionPane.YES_NO_OPTION) != JOptionPane.OK_OPTION) {
								return;
							}						
						}	
						WordList.fillFile(this, progressbar, btn_start, sp_length, tf_file.getText(), charset, true, tf_continue.getText());
					} else if (rb_mysql.isSelected()) {
						WordList.fillDatabase(this, progressbar, btn_start, sp_length, tf_hostname.getText(), tf_username.getText(), new String(tf_password.getPassword()), charset, tf_continue.getText());
					}
							
					btn_start.setText("Stop");
				} if (tabbedPane.getSelectedIndex() == 1) {
					WordList.loadDatabase();
					
					btn_start.setText("Stop");
				}
			}	
		} else if (e.getActionCommand().equals(Statics.ACTION_CONTINUE)) {
			if (cb_continue.isSelected()) {
				if (rb_mysql.isSelected()) {
					if (checkSettings()) {
						Database.setCredentials(tf_hostname.getText(), tf_username.getText(), String.valueOf(tf_password.getPassword()));
						String lastWord = WordList.getLastWord(true);
						if (lastWord == null) {
							tf_continue.setText("No words found");
						} else {
							tf_continue.setText(lastWord);
						}
						tf_continue.setVisible(true);
					} else {
						cb_continue.setSelected(false);
					}
				}				
			} else {
				tf_continue.setText("");
				tf_continue.setVisible(false);
			}
			pack();
		} else if (e.getActionCommand().equals(Statics.ACTION_CHOOSEFOLDER)) {
			setLookAndFeel(false);
			JFileChooser chooser = null;
			String filePath = "";
			if (tf_file.getText().trim().contains(Statics.NAME_WORDLIST)) {
				filePath = tf_file.getText().trim().substring(0, tf_file.getText().trim().lastIndexOf(File.separator));	
			}					
			if (new File(filePath).exists() && new File(filePath).isDirectory()) {
				chooser = new JFileChooser(filePath);
			} else {
				chooser = new JFileChooser();
			}
			setLookAndFeel(true);
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			chooser.setAcceptAllFileFilterUsed(false);

			if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
				tf_file.setText(chooser.getSelectedFile().getAbsolutePath() + File.separator + Statics.NAME_WORDLIST);
			}
		} else if (e.getActionCommand().equals(Statics.ACTION_OUTPUTMYSQL)) {
			rb_mysql.setSelected(true);
			rb_file.setSelected(false);
			
			panel_mysql.setVisible(true);
			panel_file.setVisible(false);
			
			pack();
		}  else if (e.getActionCommand().equals(Statics.ACTION_OUTPUTFILE)) {
			rb_mysql.setSelected(false);
			rb_file.setSelected(true);
			
			panel_mysql.setVisible(false);
			panel_file.setVisible(true);
			
			pack();
		} else if (e.getActionCommand().equals(Statics.ACTION_TESTMYSQL)) {			
			Database.setCredentials(tf_hostname.getText(), tf_username.getText(), String.valueOf(tf_password.getPassword()));
			try {		
				Connection dbConnection = Database.connect();
				if (dbConnection != null && dbConnection.isValid(Statics.TIMEOUT_MYSQL)) {
					lbl_test.setText("Connected");
				} else {
					lbl_test.setText("Not Connected");
				}
				lbl_test.setVisible(true);
			} catch (Exception ex) {
				Log.error(ex);
			}			
		}

	}	
}
